package com.archives.blog.web.erp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.archives.blog.common.domain.entity.Article;
import com.archives.blog.common.domain.pojo.request.ArticleChannelModifyRequest;
import com.archives.blog.common.domain.pojo.request.ArticleModifyRequest;
import com.archives.blog.common.domain.pojo.request.ArticleQueryRequest;
import com.archives.blog.common.domain.pojo.result.Result;
import com.archives.blog.manager.ArticleChannelManager;
import com.archives.blog.manager.ArticleManager;
import com.archives.blog.web.BaseSupportController;

/**
 * 文章后台
 * 
 * @author chh
 *
 */
@Controller
@RequestMapping("/erp/article")
public class ERPArticleController extends BaseSupportController{

	/** 日志管理*/
	private static Logger logger = LoggerFactory.getLogger(ERPArticleController.class);

	/** 文章管理*/
	@Autowired
	private ArticleManager articleManager;

	/** 文章频道管理*/
	@Autowired
	private ArticleChannelManager articleChannelManager;

	/**
	 * 文章分页查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/query")
	@ResponseBody
	public Page<Article> query(@RequestBody ArticleQueryRequest request){
		Page<Article> page = null;
		try {
			page = articleManager.getPage(request);
		} catch (Exception e) {
			logger.error("文章查询失败", e);
		}
		return page;
	}
	
	/**
	 * 文章保存
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Result<Boolean> modify(@RequestBody ArticleModifyRequest request){
		Result<Boolean> result = new Result<Boolean>();
		try {
			if (request.getArticleId() == null || request.getArticleId() == 0) {
				articleManager.add(request);
			} else {
				articleManager.modify(request);
			}
			result.setResultObject(true);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setErrorMsg(e.getMessage());
			logger.error("文章保存失败", e);
		}
		return result;
	}
	
	/**
	 * 文章频道保存
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/channel/save")
	@ResponseBody
	public Result<Boolean> channelSave(@RequestBody ArticleChannelModifyRequest request){
		Result<Boolean> result = new Result<Boolean>();
		try {
			if (request.getChannelId() == null || request.getChannelId() == 0) {
				articleChannelManager.add(request);
			} else {
				articleChannelManager.modify(request);
			}
			result.setResultObject(true);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setErrorMsg(e.getMessage());
			logger.error("文章频道保存失败", e);
		}
		return result;
	}
}
